All Questions
12 questions
6votes
2answers
186views
Templated division by a power of 2
I've implemented a div_by_power_of_2() function, which lets me force the compiler to use left-shifting rather than proper division, in cases when the developer know ...
5votes
3answers
261views
Basic binary number container
Summary: a templated class, whose main purpose is to store a binary representation of a decimal number. I choose array of booleans as a storage, since bitset was forbidden. One of the requirements was ...
15votes
2answers
1kviews
Program that guesses your number using bitwise operations
I got the idea for this program from this site. functions.cpp: ...
4votes
1answer
2kviews
Copy arbitrary number of bits at arbitrary offset from buffer to another buffer
I have just written a function copy_lowbits_off to copy any number of bits (not bytes) from a source buffer to a destination buffer. The function also supports ...
3votes
1answer
775views
Implementing binary output to a file in C++11
I would like to have a class which supports outputting bits into a file. This is what I came up with but I fear there are many poor decisions. Should I inherit from a ...
11votes
2answers
5kviews
Mostly portable 128 by 64 bit division
I wrote this out of curiosity. It is based on the theory behind Knuth's Algorithm D and is intended to emulate the behavior of the x86 div instruction (though the ...
3votes
2answers
1kviews
Emulating __uint128_t
I developed a lot of code locally with __uint128_t only to find out that it is not available on the target platform. I am now trying to write my own struct that ...
8votes
3answers
310views
Check if bit is only set once in a vector of int
I have a vector of uint16_t and I want to check if there is a bit which is only set in one vector. I then get its position within the vector as well as from the ...
3votes
2answers
547views
Function for splitting an integer into smaller values
This is the function I've implemented in an answer to this question. I've tried to make this as simple and idiomatic as possible, using C++11. Could this still be improved in any way? ...
9votes
1answer
660views
Optimizing multiplication of two polynomials in GF(2^32)
I have the following method for multiplying two polynomials in GF(232): ...
7votes
2answers
11kviews
Bit packing and unpacking
I needed a variadic function to pack and unpack bits into integer types. This is my first attempt: ...
2votes
2answers
665views
Copy and bit-invert ints in an std::vector
I want a second copy of an std::vector in which all elements are inverted, e.g. 0x01 in the first vector should be ...